{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "import statsmodels.api as sm\n",
    "import statsmodels.formula.api as smf\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>DJIA</th>\n",
       "      <th>SPX</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1997.10.24</th>\n",
       "      <td>7715</td>\n",
       "      <td>942</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.10.31</th>\n",
       "      <td>7442</td>\n",
       "      <td>915</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.11.7</th>\n",
       "      <td>7581</td>\n",
       "      <td>928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.11.14</th>\n",
       "      <td>7572</td>\n",
       "      <td>928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.11.21</th>\n",
       "      <td>7881</td>\n",
       "      <td>963</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.11.28</th>\n",
       "      <td>7823</td>\n",
       "      <td>955</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.12.5</th>\n",
       "      <td>8149</td>\n",
       "      <td>984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.12.12</th>\n",
       "      <td>7838</td>\n",
       "      <td>953</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.12.19</th>\n",
       "      <td>7756</td>\n",
       "      <td>947</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.12.26</th>\n",
       "      <td>7679</td>\n",
       "      <td>936</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            DJIA  SPX\n",
       "Date                 \n",
       "1997.10.24  7715  942\n",
       "1997.10.31  7442  915\n",
       "1997.11.7   7581  928\n",
       "1997.11.14  7572  928\n",
       "1997.11.21  7881  963\n",
       "1997.11.28  7823  955\n",
       "1997.12.5   8149  984\n",
       "1997.12.12  7838  953\n",
       "1997.12.19  7756  947\n",
       "1997.12.26  7679  936"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_table('./test4.txt', sep = ' ', header = 0, index_col = 0)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>const</th>\n",
       "      <th>DJIA</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1997.10.24</th>\n",
       "      <td>1.0</td>\n",
       "      <td>7715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.10.31</th>\n",
       "      <td>1.0</td>\n",
       "      <td>7442</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.11.7</th>\n",
       "      <td>1.0</td>\n",
       "      <td>7581</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.11.14</th>\n",
       "      <td>1.0</td>\n",
       "      <td>7572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.11.21</th>\n",
       "      <td>1.0</td>\n",
       "      <td>7881</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.11.28</th>\n",
       "      <td>1.0</td>\n",
       "      <td>7823</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.12.5</th>\n",
       "      <td>1.0</td>\n",
       "      <td>8149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.12.12</th>\n",
       "      <td>1.0</td>\n",
       "      <td>7838</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.12.19</th>\n",
       "      <td>1.0</td>\n",
       "      <td>7756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997.12.26</th>\n",
       "      <td>1.0</td>\n",
       "      <td>7679</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            const  DJIA\n",
       "Date                   \n",
       "1997.10.24    1.0  7715\n",
       "1997.10.31    1.0  7442\n",
       "1997.11.7     1.0  7581\n",
       "1997.11.14    1.0  7572\n",
       "1997.11.21    1.0  7881\n",
       "1997.11.28    1.0  7823\n",
       "1997.12.5     1.0  8149\n",
       "1997.12.12    1.0  7838\n",
       "1997.12.19    1.0  7756\n",
       "1997.12.26    1.0  7679"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = data[['DJIA']]\n",
    "y = data[['SPX']]\n",
    "X = sm.add_constant(x)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "const    166.082832\n",
       "DJIA       0.100601\n",
       "dtype: float64"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_model = sm.OLS(y,X)\n",
    "sm_result = sm_model.fit()\n",
    "sm_result.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9904333423452636"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_result.rsquared"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1001.07463095])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_result.predict([1, 8300])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "const    166.082832\n",
       "DJIA       0.100601\n",
       "dtype: float64"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_model = smf.ols(formula='SPX ~ DJIA', data = data)\n",
    "sm_reslut = sm_model.fit()\n",
    "sm_result.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9904333423452636"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_result.rsquared"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sm_result.predict(pd.DataFrame([{8300:'DJIA'}]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "166.08283214871528"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sk_model = LinearRegression()\n",
    "sk_model.fit(x, y)\n",
    "sk_model.intercept_[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.10060142154182612"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sk_model.coef_[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9904333423452637"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sk_model.score(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1001.07463095]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sk_model.predict([[8300]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
